﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Windows;
using System.Windows.Controls;
using Granite.Xaml;
using Microsoft.Win32;
using Rifts.Creator;
using Rifts.Models.Characters;

namespace Rifts
{
	public class PrimaryViewModel : ViewModelBase
	{
		/// <summary>
		/// This is set to the View-Model being used by the matching content frame
		/// </summary>
		public ViewModelBase LoadedContent
		{
			get { return Get<ViewModelBase>("LoadedContent"); }
			set { Set(value, "LoadedContent"); }
		}

		public void CreateNew()
		{
			//TODO: Primpt for unsaved changes
			LoadedContent = new CreatorViewModel(this);
		}

		public void Print(Character character)
		{
			var printDialog = new PrintDialog();
			if (printDialog.ShowDialog() != true)
				return;

			var paginator = new CharacterSheetPaginator(character, new Size(printDialog.PrintableAreaWidth,
	printDialog.PrintableAreaHeight));

			printDialog.PrintDocument(paginator, "Rifts Character Generator");

		}

		public void Save(Character character)
		{
			var dcs = new DataContractSerializer(typeof(Character));
			var sfd = new SaveFileDialog();
			sfd.DefaultExt = ".rfts-character";
			sfd.Filter = "Rifts Character (.rfts-character)|*.rfts-character";
			if (sfd.ShowDialog() == true)
			{
				//TODO: Add error handling
				using (var fileStream = new FileStream(sfd.FileName, FileMode.Create))
				{
					dcs.WriteObject(fileStream, character);
				}
			}

		}

	}
}